package bhbt.controller.admin.member;


import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Member;
import bhbt.service.MemberService;
import bhbt.taglib.pagetag.Pagination;
import bhbt.utils.MapUtil;
import bhbt.utils.SysConst;






/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class MemberYesActiveListController extends BaseController {
    @Resource
    private MemberService memberService;
   
   
    @RequestMapping("admin/member/memberYesActiveList.shtml")
    public String list(HttpServletRequest request) throws Exception{
        HashMap formMap = getParameterMap(request);
        formMap.put("accountActive", "T");			//未激活
        
        
        String loginNameDiy=MapUtil.getString(formMap, "loginNameDiy");
        if(loginNameDiy!=null){
        	 formMap.put("where", "1=1 and a.loginName like '%"+loginNameDiy+"%'");			//未激活
        }
       
        formMap.put("pagination.orderBy","activeDate desc");
        
        Pagination page = memberService.queryForPageList("memberForPage",formMap);
       
        request.setAttribute(Pagination.pageBeanName, page);
        
        
        
        return "admin/member/memberYesActiveList";
    }

    @RequestMapping("admin/member/memberYesActiveShow.shtml")
    public String show(HttpServletRequest request, HttpSession session) throws Exception{
    	Map formMap = getParameterMap(request);
    	int id = MapUtil.getIntValue(formMap, "id");
    	Member member =memberService.getByKey(id);
    	session.setAttribute(SysConst.MEMBER, member);
    	
        return "redirect:/member/userManage.shtml";
    }
    
    /**
     * 是否冻结
     * @param request
     * @return
     * @throws Exception
     */
     
    @RequestMapping("admin/member/memberYesActiveUse.shtml")
    public String use(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Member member =memberService.getByKey(id);
        	Map mapMember = new HashMap();
        	mapMember.put("id",id);
        	  
        	if(member.getAccountUse().equals("F")){
        		 
        		mapMember.put("accountUse","T");
        		 
        	}else{
        		 
        		mapMember.put("accountUse","F");
        		
        	}
        	memberService.update(mapMember);
        }
        return "redirect:memberYesActiveList.shtml";
       
    }
    
    /**
     * 是否服务中心
     * @param request
     * @return
     * @throws Exception
     */
     
    @RequestMapping("admin/member/memberYesAccountCenter.shtml")
    public String center(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Member member =memberService.getByKey(id);
        	Map mapMember = new HashMap();
        	mapMember.put("id",id);
        	  
        	if(member.getAccountCenter().equals("F")){
        		 
        		mapMember.put("accountCenter","T");
        		 
        	}else{
        		 
        		mapMember.put("accountCenter","F");
        		
        	}
        	memberService.update(mapMember);
        }
        return "redirect:memberYesActiveList.shtml";
       
    }
    /**
     * 工资状态
     * @param request
     * @return
     * @throws Exception
     */
     
    @RequestMapping("admin/member/memberYesActiveWage.shtml")
    public String wage(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Member member =memberService.getByKey(id);
        	Map mapMember = new HashMap();
        	mapMember.put("id",id);
        	if(member.getStateWage().equals("F")){
        		mapMember.put("stateWage","T"); 
        	}else{
        		 
        		mapMember.put("stateWage","F"); 
        		
        	}
        	memberService.update(mapMember);
        }
        return "redirect:memberYesActiveList.shtml";
       
    }
     
      
    
    @RequestMapping("admin/member/memberYesActiveListOut.shtml")
    public void out(HttpServletRequest request, HttpServletResponse response) throws Exception{
        HashMap formMap = getParameterMap(request);
        String remarkDiy = MapUtil.getString(formMap, "remarkDiy");
          
        
        formMap.put("memberLoginName",MapUtil.getString(formMap, "loginNameDiy"));  
       
        if(remarkDiy!=null&&!remarkDiy.equals("")){
    		formMap.put("where", "1=1 and a.remark like '%"+remarkDiy+"%'");			//未激活
    	}
        
        List list =memberService.queryForList("memberForList",formMap);
       
        String realPath = request.getRealPath("model/person.xls");

		// 读取EXCEL文件
		FileInputStream fileIn=new FileInputStream(realPath);
		POIFSFileSystem fs=new POIFSFileSystem(fileIn);

		HSSFWorkbook wb=new HSSFWorkbook(fs);
		HSSFSheet sheet=wb.getSheetAt(0);
		
		
		//List集合里的是hashMap
        Map map = null;
        
        int num=1;
		
        for(int i=0;i<list.size();i++){
        	map=(HashMap)list.get(i);
        	
        	HSSFRow row=sheet.createRow(num);
			String loginName = (String)map.get("loginName");
			System.out.println("----------memberLoginName:"+loginName);
			
			HSSFCell lie0=row.createCell((short)0);
			lie0.setCellValue(map.get("id").toString());
			
			
			HSSFCell lie1=row.createCell((short)1);
			lie1.setCellValue((String)map.get("loginName"));
			
			HSSFCell lie2=row.createCell((short)2);
			lie2.setCellValue((Double)map.get("money"));
			
			
			HSSFCell lie3=row.createCell((short)3);
			lie3.setCellValue(map.get("activeDate").toString());
			
			HSSFCell lie4=row.createCell((short)4);
			lie4.setCellValue((Double)map.get("gameWage1"));
			
			HSSFCell lie5=row.createCell((short)5);
			lie5.setCellValue((Double)map.get("gameWage2"));
			
			HSSFCell lie6=row.createCell((short)6);
			lie6.setCellValue((Double)map.get("gameWage3"));
			
			 
			HSSFCell lie7=row.createCell((short)7);
			lie7.setCellValue((String)map.get("accountUse"));
			
			HSSFCell lie8=row.createCell((short)8);
			lie8.setCellValue((String)map.get("accountXia"));
			
			
			 
			num++;

        }
		 


		/***************************  文件下载  **************************************/
		
		//下载时显示的名称
		SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddHHmmss"); 
		String fileName = "提现("+formatter2.format(new Date())+")";
	
		// 设置文件下载
		
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "iso8859-1") + ".xls");
	
		// 定义输出流
		ServletOutputStream out= response.getOutputStream();
		try {
			wb.write(out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception("文件输出出错！");
		} finally {
			fileIn.close();
			fileIn=null;
				
			out.close();
			out = null;
		} 
    }
}
